# -*- coding: utf-8 -*-

from rest_framework.response import Response
from rest_framework import status


class JsonResponse(Response):
    """
    返回JSON格式，当`paging`为True时，采用分页形式返回
    """

    def __init__(
        self,
        code=status.HTTP_200_OK,
        paginate=False,
        data=[],
        msg="暂无数据",
        page=1,
        limit=10,
        total=0,
        status=None,
        template_name=None,
        headers=None,
        exception=False,
        content_type=None,
    ):
        data = {
            "code": code,
            "data": data,
            "msg": msg,
            "total": len(data) if isinstance(data, list) else 1,
        }
        if paginate:
            data["msg"] = msg
            data["page"] = page
            data["limit"] = limit
            data["total"] = total
        super().__init__(
            data=data,
            status=status,
            template_name=template_name,
            headers=headers,
            exception=exception,
            content_type=content_type,
        )
